package com.hgts.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgts.common.R;
import com.hgts.pojo.Book;
import com.hgts.pojo.dto.BookDto;
import com.hgts.pojo.dto.TypeDto;
import com.hgts.service.IBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凡哥哥
 * @since 2023-04-27
 */
@RestController
@RequestMapping("/book")
@Slf4j
public class BookController {
    @Autowired
    private IBookService bookService;


    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name){

        return bookService.pageInfo(page,pageSize,name);
    }

    @GetMapping("/page/hot")
    public R<Page> pageHot(Integer page, Integer pageSize, String name){
        Page<Book> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Book::getIsHot,1);
        bookService.page(page1, queryWrapper);

        return R.success(page1);
    }

    /**
     * 获取图书类型
     * @return
     */
    @GetMapping("/getType")
    public R<List<TypeDto>> getType(){
        return bookService.getType();
    }

    /**
     * 添加图书信息
     * @param book
     * @return
     */
    @PostMapping("/addBook")
    public R<String> addBook(@RequestBody Book book){
        return bookService.addBook(book);
    }

    /**
     * 获取图书信息进行回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Book> getBook(@PathVariable int id){
        return bookService.getBook(id);
    }

    /**
     * 修改图书信息
     * @param book
     * @return
     */
    @PutMapping
    public R<String> updateBook(@RequestBody Book book){
        return bookService.updateBook(book);
    }

    /**
     * 删除和批量删除
     */
    @DeleteMapping
    public R<String> deleteBooks(int[] ids){
        return bookService.deleteBooks(ids);
    }

    /**
     * 借阅图书
     * @param bookDto
     * @return
     */
    @PutMapping("/update")
    public R<String> libraryBook(@RequestBody BookDto bookDto){
        log.info("图书借阅信息{}",bookDto);
        bookDto.setStartTime(bookDto.getTimeArr()[0]);
        bookDto.setEndTime(bookDto.getTimeArr()[1]);
        return bookService.libraryBook(bookDto);
    }

    @GetMapping("/returnBook/{id}")
    public R<String> libraryBook(@PathVariable int id){

        return bookService.returnBook(id);
    }

    /**
     * 热书推荐
     *
     */
    @PutMapping("/hot")
    public R<String> hotBook(int id){
        LambdaUpdateWrapper<Book> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Book::getIsHot,1);
        updateWrapper.eq(Book::getId,id);
        boolean update = bookService.update(updateWrapper);
        if (update) {
            return R.success("推荐成功");
        }
        return R.error("服务器异常");
    }


    /**
     * 取消热书推荐
     *
     */
    @PutMapping("/notHot")
    public R<String> notHotBook(int id){
        LambdaUpdateWrapper<Book> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Book::getIsHot,0);
        updateWrapper.eq(Book::getId,id);
        boolean update = bookService.update(updateWrapper);
        if (update) {
            return R.success("取消推荐成功");
        }
        return R.error("服务器异常");
    }

    /**
     * 小程序根据类型id差图书
     *
     */
    @GetMapping("/wx/{id}")
    public R<Page> getBookApp(@PathVariable Long id,@RequestParam("_page") Integer page, @RequestParam("_limit") Integer pageSize){
        return bookService.pageInfoWx(id,page,pageSize);
    }
}

